package com.wulian.controller;

import com.github.pagehelper.PageInfo;
import com.wulian.entities.Patient;
import com.wulian.result.CodeStatus;
import com.wulian.result.ResponseResult;
import com.wulian.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/patient")
@Api( tags = "病人操作接口")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @GetMapping("/")
    @ApiOperation("获取所有病人信息")
    public ResponseResult<List<Patient>> getPatients(){
        try {
            List<Patient> patients = patientService.getAll();
            if (patients==null){
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查询到相关数据");
            }
            else {
                return new ResponseResult<>(CodeStatus.OK,"查询成功",patients);
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }

    }

    @ApiOperation("分页查询病人信息")
    @GetMapping("/{pageNum}/{size}")
    public ResponseResult<PageInfo<Patient>> getPatientsByPage(@PathVariable Integer pageNum,@PathVariable Integer size){
        if(pageNum<=0&&size<=0){
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST,"参数错误");
        }

        PageInfo<Patient> patients = patientService.getAllByPage(pageNum, size);
        try {
            if (patients==null){
                return  new ResponseResult<>(CodeStatus.NOT_FOUND, "未查询到相关数据");
            }
            else {
                return new ResponseResult<>(CodeStatus.OK,"查询成功",patients);
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }

    }

    @ApiOperation("根据病人ID查询")
    @GetMapping("/{id}")
    public ResponseResult<Patient> getPatientById(@PathVariable Long id){
        if(id<=0){
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST,"参数错误");
        }
        try {
            Patient patient = patientService.getPatientById(id);
            if (patient!=null){
                return new ResponseResult<>(CodeStatus.OK,"查询成功",patient);
            }else{
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查询到相关数据");
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }

    }

    @ApiOperation("添加病人信息")
    @PostMapping("/")
    public ResponseResult<Object> addPatient(@RequestBody Patient patient){
        try {
            patientService.addPatient(patient);
            return new ResponseResult<>(CodeStatus.OK,"添加成功");
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

    @ApiOperation("修改病人信息")
    @PutMapping("/")
    public ResponseResult<Object> updatePatient(@RequestBody Patient patient){
        try {
            patientService.updatePatient(patient);
            return new ResponseResult<>(CodeStatus.OK,"修改成功");
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseResult<Object> deletePatient(@PathVariable Long id){
        try {
            patientService.deletePatientById(id);
            return new ResponseResult<>(CodeStatus.OK,"删除成功");
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }

}
